<?php 
namespace Home\Model;
use Think\Model;
class CategoryModel extends Model {
    public function getCate()
    {   //继承Model里面有select方法，当前对象的select方法，又根据模型名和表名是对应的，能直接查到category表的数据
        $cate = $this->select();

        /*var_dump($cate);
        array (size=8)
          0 => 
            array (size=3)
              'id' => string '1' (length=1)
              'cname' => string 'layui' (length=5)
              'fid' => string '4' (length=1)
          1 => 
            array (size=3)
              'id' => string '2' (length=1)
              'cname' => string 'layer弹层' (length=11)
              'fid' => string '4' (length=1)*/
        
        //先把两个父ID循环出来
        $tempArr = [];

        foreach ($cate as $rowarr) {
            if($rowarr['fid']==0){
                $tempArr[] = $rowarr;
            }
        }
        //var_dump($tempArr);
        
        foreach ($tempArr as $k => $father) {
            foreach ($cate as $v) {
                if($v['fid']==$father['id']){
                    $tempArr[$k]['son'][]=$v;
                    
                 }
            }
        }
        //var_dump($tempArr);
        return $tempArr;
        /*array (size=2)
          0 => 
            array (size=4)
              'id' => string '4' (length=1)
              'cname' => string 'Layui相关' (length=11)
              'fid' => string '0' (length=1)
              'son' => 
                array (size=3)
                  0 => 
                    array (size=3)
                      ...
                  1 => 
                    array (size=3)
                      ...
                  2 => 
                    array (size=3)
                      ...*/
    }

}

?>